package com.luyuan.crm.sale.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Address;
import com.luyuan.crm.member.model.Region;
import com.luyuan.crm.member.service.RegionService;
import com.luyuan.crm.sale.model.Delivery;
import com.luyuan.crm.sale.model.Order;
import com.luyuan.crm.sale.model.OrderStatus;
import com.luyuan.crm.sale.model.OrderType;
import com.luyuan.crm.sale.service.OrderService;
import com.ruijie.blueprint.spring.context.ApplicationContextHolder;
import com.ruijie.util.StringUtils;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.action.WizardAccessor;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;

@Component("orderCheckerAction")
@Scope("view")
public class OrderCheckerAction extends BaseAction implements InitializingBean {

    @Resource(name = "orderService")
    private OrderService orderService;

    @Resource(name = "regionService")
    private RegionService regionService;

    private Order order = null;

    private String uuid = null;

    private String remarks;

    public void verifyOrder(String flag) {
        if (flag.equals("verify")) {
            orderService.verifyOrder(order, remarks);
        }
        else if (flag.equals("return")) {
            orderService.returnOrder(order, remarks);
        }
        else if (flag.equals("cancel")) {
            orderService.cancelOrder(order, remarks);
        }
    }

    public String startWizard(Order order) {
        WizardAccessor wizardAccessor = (WizardAccessor) ApplicationContextHolder.getApplicationContext().getBean(
                "wizardAccessor");

        setRequestAttribute("order", order, true);
        if (order.getOrderType() == OrderType.SINGLE_PRODUCT) {
            return wizardAccessor.start("singleOrderWizard", "update");
        }
        else {
            return wizardAccessor.start("orderWizard", "update");
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (getRequestParameter("uuid") != null && order == null) {
            String id = getRequestParameter("uuid");
            order = orderService.getById(id);
            if (order.getDelivery() == null) {
                order.setDelivery(new Delivery());
                order.getDelivery().setDeliveryAddress(new Address());
            }
            else if (order.getDelivery().getDeliveryAddress() == null) {
                order.getDelivery().setDeliveryAddress(new Address());
            }
        }
    }

    /**
     * 提交订单的状态的列表
     * 
     * @return
     */
    public DataModel getOrderTableModel() {
        TablePage tablePage = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<org.hibernate.criterion.Order> orders = new ArrayList<org.hibernate.criterion.Order>();
        for (TableOrder tableOrder : tableOrders) {
            org.hibernate.criterion.Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(Order.class);
        RestrictionTemplate r = new RestrictionTemplate(false);
        c.add(r.eq("orderStatus", OrderStatus.CREATED));

        if (StringUtils.isNotEmpty(uuid)) {
            c.add(r.eq("id", uuid));
        }

        Page page = orderService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void resetPostCode(ActionEvent event) {
        String province = null, city = null, region = null;
        UIInput uiInput = (UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent("orderForm:province");
        if (uiInput != null) {
            province = String.valueOf(uiInput.getValue());
        }

        uiInput = (UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent("orderForm:city");
        if (uiInput != null) {
            city = String.valueOf(uiInput.getValue());
        }

        uiInput = (UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent("orderForm:region");
        if (uiInput != null) {
            region = String.valueOf(uiInput.getValue());
        }

        Region r = regionService.getByRegion(province, city, region);
        if (r != null) {
            order.getDelivery().getDeliveryAddress().setPostCode(r.getId());
        }
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
}
